import Mock from 'mockjs'
let regionList = []
for (let i = 0; i < 100; i++) {
    regionList.push(
        Mock.mock({
            shopRegionName: '@cname',
            'regionType|1-3': 1,
            provinceName: '@province',
            cityName: '@city',
            districtName: '@county'
        })
    )
}

let provinces = []
for (let i = 0; i < 5; i++) {
    provinces.push(
        Mock.mock({
            id: '@increment',
            text: '@province'
        })
    )
}
let citys = []
for (let key in provinces) {
    let province = provinces[key]
    province.children = []
    for (let i = 0; i < 5; i++) {
        let city = Mock.mock({
            id: '@increment',
            text: '@city'
        })
        citys.push(city)
        province.children.push(city)
    }
}
let dists = []
for (let key in citys) {
    let city = citys[key]
    city.children = []
    for (let i = 0; i < 5; i++) {
        let dis = Mock.mock({
            id: '@increment',
            text: '@county'
        })
        dists.push(dis)
        city.children.push(dis)
    }
}

export default [
    {
        url: '/addRegion.json',
        method: 'post',
        statusCode: 200,
        response: (query) => {
            regionList.unshift({
                shopRegionName: query.shopRegionName,
                regionType: query.regionType,
                provinceName: provinces.find((v) => v.id == query.provinceRid).text,
                cityName: citys.find((v) => v.id == query.cityRid).text,
                districtName: dists.find((v) => v.id == query.districtRid).text
            })
            return {
                code: 200,
                msg: 'OK',
                data: {}
            }
        }
    },
    {
        url: '/getRegionList.json',
        method: 'get',
        statusCode: 200,
        response: (query) => {
            let deslist = regionList
                .filter((v) => (query.region_type ? query.region_type == v.regionType : true))
                .filter((v) => (query.region_name ? query.region_name == v.shopRegionName : true))
            return {
                code: 200,
                msg: 'OK',
                data: {
                    totalItemNum: deslist.length,
                    page: 1,
                    pageSize: query.size,
                    items: deslist.slice(query.start, parseInt(query.start) + parseInt(query.size))
                }
            }
        }
    },
    {
        url: '/getRegionByPid.json',
        method: 'get',
        statusCode: 200,
        response: (query) => {
            return {
                code: 200,
                msg: 'OK',
                data: query.pid ? provinces.concat(citys).find((v) => v.id == query.pid)?.children : provinces
            }
        }
    }
]
